import tracker from '../utils/tracker.js';
import onload from '../utils/onload.js';
// src/monitor/utils/onload.js

export default function blankScreen() {
  let warpperElements = ['html', 'body', '#container', ".content"]
  let enptyPoints = 0
  function getSelector(element) {
    if (element && element.id) {
      return "#" + element.id
    } else if (element && element.className) {
      //  a b c => .a .b .c
      return "." + element.className.split(' ').filter(item => !!item).join('.')
    } else {
      return element && element.nodeName.toLowerCase()
    }
  }
  //  是包裹元素++
  function isWrapper(element) {
    let selector = getSelector(element)
    if (warpperElements.indexOf(selector) != -1) {
      enptyPoints++
    }
  }
  // 当整个页面渲染完成了才去判断是否是白屏
  onload(function () {
    for (let i = 1; i <= 18; i++) {
      let xElement = document.elementsFromPoint(window.innerWidth * i / 10, window.innerHeight / 2)
      let yElement = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight * i / 10)
      isWrapper(xElement[0])
      isWrapper(yElement[0])
    }
    // console.log("enptyPoints", enptyPoints)
    if (enptyPoints >= 16) {
      let centerElements = document.elementsFromPoint(window.innerWidth / 2, window.innerHeight / 2)
      tracker.send({
        kind: "stability",
        type: "blank",
        blankCount: enptyPoints, // 空白点
        screen: window.screen.width + "*" + window.screen.height, // 屏幕尺寸
        viewPoint: window.innerWidth + "*" + window.innerHeight, // 视口尺寸
        selector: getSelector(centerElements[0]),
        page: window.location.href, // 页面地址
        message: "页面空白点大于16个"
      })
    }
  });
}